package com.ruoyi.mateProcurement.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.mapper.CompanyMapper;
import com.ruoyi.mateProcurement.domain.MateProcurement;
import com.ruoyi.mateProcurement.domain.MateProcurementModel;
import com.ruoyi.mateProcurement.service.MateProcurementService;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.procurement.domain.ProcurementOrder;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.sale.service.ISaleorderService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateProcurement")
public class MateProcurementController extends BaseController {

    @Autowired
    MateProcurementService mateProcurementService;

    @Autowired
    ISysUserService userService;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateProductService mateProductService;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    ISaleorderService saleorderService;

    @Autowired
    ProductMapper productMapper;

    /**
     * 新增保存入库信息
     */
    @RequiresPermissions("business:procurement:add")
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MateProcurement mateProcurement) {
        if (mateProcurement.getMateProcurementModels() == null) {
            return AjaxResult.error("请选择需要入库的原材料");
        }

        String mateOrdernum = mateProcurement.getMateOrdernum();
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("mateOrdernum", mateOrdernum);
        resMap.put("deptId", companyId);
        MateProcurement mateProcurement1 = mateProcurementService.selectMateProcurement(resMap);
        if (mateProcurement1 != null) {
            //使用SimpleDateFormat的parse()方法生成Date
            SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
            Date dates = null;
            try {
                dates = a.parse(a.format(mateProcurement1.getPurchaseDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String s = mateProcurementService.orderNum(dates, 0);
            mateProcurement.setMateOrdernum(s);
        }
        mateProcurementService.insertMatrProcurement(mateProcurement);
        return AjaxResult.success("添加成功");

    }

    /**
     * 查询订单入库信息列表
     */
    @RequiresPermissions("business:procurement:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "beginTime", required = false) String beginTime,
                              @RequestParam(value = "endTime", required = false) String endTime) {
        MateProcurement procurement = new MateProcurement();
        procurement.setBeginTime(beginTime);
        procurement.setEndTime(endTime);
        startPage();
        procurement.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateProcurement> list = mateProcurementService.selectMateProcurementList(procurement);
        return getDataTable(list);
    }

    @GetMapping("/remove/{mateProcurementId}")
    public AjaxResult removeProcurement(@PathVariable("mateProcurementId") Long mateProcurementId) {
        mateProcurementService.deleteProcurement(mateProcurementId);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("/edit")
    public AjaxResult updateProcurement(@RequestBody MateProcurement mateProcurement) {
        mateProcurementService.updateProcurement(mateProcurement);
        return AjaxResult.success("修改成功");
    }

    @GetMapping("/selectProductModelLists")
    @ResponseBody
    public TableDataInfo selectProductModelLists(@RequestParam(value = "materialId", required = false) Long materialId,
                                                 @RequestParam(value = "mateProductName", required = false) String mateProductName,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        startPage();
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        MaterialProduct product = new MaterialProduct();
        product.setMaterialId(materialId);
        product.setCompanyId(deptId);
        product.setMateProductName(mateProductName);
        product.setMateProductType(mateProductType);
        return getDataTable(mateProcurementService.selectProductLists(product));
    }

    /**
     * 添加 采购订单入库信息
     */
    @GetMapping("/selectInformation")
    public Map<String, Object> add() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();
        //使用SimpleDateFormat的parse()方法生成Date
        SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
        Date dates = null;
        try {
            dates = a.parse(a.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        resMap.put("procurementWarehousingName", mateProcurementService.orderNum(dates, 0));
        //用户主键
        resMap.put("sysUser", sysUser.getDeptId());
        resMap.put("userName", sysUserMapper.selectUserBycompanyId(sysUser.getCompanyId()));
        //供应商
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setCompanyId(sysUser.getCompanyId());
        resMap.put("supplier", mateManufactuterService.selectMateManufactuter(materialManufactuter));
        //用户
        //resMap.put("user", userService.selectSalesmanList(sysUser.getCompanyId()));
        //税率
        Company company = companyMapper.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("taxRate", company.getTaxRate());
        return resMap;
    }

    @GetMapping("/selectEdit")
    public Map<String, Object> selectEdit(@RequestParam(value = "mateProcurementId", required = false) Long mateProcurementId) {
        Map<String, Object> resMap = new HashMap<>();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Company company = companyMapper.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("taxRate", company.getTaxRate());

        MateProcurement mateProcurement = new MateProcurement();
        mateProcurement.setMateProcurementId(mateProcurementId);
        mateProcurement.setDeptId(sysUser.getCompanyId());
        MateProcurement mateProcurement1 = mateProcurementService.selectMateProcurementBy(mateProcurement);
        // List<MateProcurementModel> mateProductModels = mateProcurementService.selectMateProcumentModelBy(mateProcurementId);
        resMap.put("MateProcurement", mateProcurement1);
        //  resMap.put("mateProductModels", mateProductModels);
        return resMap;

    }
}
